import { LOCAL_KEY } from '@/constant/dict';

export default {
    methods: {
        checkWizard() {
            const isNeedWizard = localStorage.getItem(LOCAL_KEY.IS_NEED_WIZARD);
            const wizardHistory = localStorage.getItem(LOCAL_KEY.IS_WIZARD_HISTORY);

            if (this.$route.path == '/wizard' && wizardHistory) {
                return this.$router.replace(wizardHistory);
            }

            // 默认进入向导页
            let wizardFlag = true;

            // 如果本地存在 IS_NEED_WIZARD，判断是否为 1 或空值，如果为真则进向导页
            if (isNeedWizard) {
                wizardFlag = ['', '1'].includes(isNeedWizard);
            }

            // 如果不需要进入向导，则清除向导配置的相关信息
            if (!wizardFlag) {
                sessionStorage.removeItem(LOCAL_KEY.IS_WIZARD_BASE_SETTING);
                sessionStorage.removeItem(LOCAL_KEY.IS_SKIP_WIZARD);
                localStorage.removeItem(LOCAL_KEY.IS_WIZARD_HISTORY);
            }

            if (wizardFlag && !this.$route.path.includes('/wizard')) {
                if (this.$route.path == '/login') {
                    this.$router.replace('/wizard');
                } else {
                    localStorage.setItem(LOCAL_KEY.IS_NEED_WIZARD, '0');
                }
            } else if (!wizardFlag && !localStorage.getItem(LOCAL_KEY.IS_AUTH_TOKEN) && this.$route.path != '/login') {
                this.$router.replace('/login');
            }
        }
    }
};
